<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      required: false
    }
  },
  data() {
    return {
      depts: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const data = await getDepartment()
      this.depts = transListToTreeData(data, 0)
    },
    handleChange(value) {
      console.log(value)
      const updateValue = value[value.length - 1]
      this.$emit('input', updateValue)
    }
  }
}
</script>

<template>
  <el-cascader
    :value="value"
    :options="depts"
    :props="props"
    separator="-"
    size="mini"
    @change="handleChange"
  />
</template>
